﻿import ConnectStrForm from "./ConnectStrForm.vue"
export default {
  components: { ConnectStrForm },
  props: ['dialogInfo'],
  data () {
    return {
      dialogTitle: '新增数据源信息',
      dialogFormVisible: false,
      labelWidth: '102px',
      dialogLoading: false,
      sourceCategoryList: [], // 数据源类型
      // 表单部分
      ruleForm: {
        source_name: '',
        source_category: '',
        connect_str: ''
      },
      // 表单规则部分
      rules: {
        source_name: [
          {required: true, message: '数据源名称不能为空', trigger: 'blur'}
        ],
        source_category: [
          {required: true, message: '数据源类型不能为空', trigger: 'change'}
        ],
        connect_str: [
          {required: true, message: '连接串不能为空', trigger: 'blur'}
        ]
      },
      connectStrDailog: {
        dialogFormVisible: false,
        category: ''
      }
    }
  },
  created () {
    this.initDialog()
    // 获取下拉框的数据源
    this.getAllSourceCategoryInfo()
  },
  methods: {
    // 获取所有数据源类型
    getAllSourceCategoryInfo () {
      this.$get('/data_source/category').then((data) => {
        let dataResult = data.data.result
        if (dataResult.code === 'success') {
          this.sourceCategoryList = dataResult.data
        } else {
          this.$message({
            message: dataResult.msg,
            type: 'warning'
          })
        }
      }).catch((error) => {
        this.$message({
          message: error,
          type: 'error'
        })
      })
    },
    // 初始化表单时
    initDialog () {
      this.dialogTitle = this.dialogInfo.operationType === 'edit' ? '编辑数据源信息' : (this.dialogInfo.operationType === 'add' ? '新增数据源信息' : '数据源信息预览')
      this.ruleForm = {
        source_name: this.dialogInfo.rowInfo ? this.dialogInfo.rowInfo.SourceName : '',
        source_category: this.dialogInfo.rowInfo ? this.dialogInfo.rowInfo.SourceCategory : '',
        connect_str: this.dialogInfo.rowInfo ? this.dialogInfo.rowInfo.ConnectStr : ''
      }
    },
    // 新增的方法
    addMethod () {
      this.dialogLoading = true
      this.$post('/icpdatasource', {
        source_name: this.ruleForm.source_name,
        source_category: this.ruleForm.source_category,
        connect_str: this.ruleForm.connect_str
      }).then(data => {
        let dataResult = data.data.result
        if (dataResult.code === 'success') {
          this.$message({
            type: 'success',
            message: '新增成功'
          })
          this.$emit('operationSuccess')
        } else {
          this.$message({
            type: 'error',
            message: dataResult.msg
          })
        }
        this.dialogLoading = false
      }).catch(error => {
        this.dialogLoading = false
        this.$message({
          type: 'error',
          message: error
        })
      })
    },
    // 编辑的方法
    editMethod () {
      this.dialogLoading = true
      this.$patch('/icpdatasource/' + this.dialogInfo.rowInfo.SourceUID, {
        source_name: this.ruleForm.source_name,
        source_category: this.ruleForm.source_category,
        connect_str: this.ruleForm.connect_str
      }).then(data => {
        let dataResult = data.data.result
        if (dataResult.code === 'success') {
          this.$message({
            type: 'success',
            message: '编辑成功'
          })
          this.$emit('operationSuccess')
        } else {
          this.$message({
            type: 'error',
            message: dataResult.msg
          })
        }
        this.dialogLoading = false
      }).catch(error => {
        this.dialogLoading = false
        this.$message({
          type: 'error',
          message: error
        })
      })
    },
    // 点击弹窗的取消按钮
    resetForm (forms) {
      this.$refs[forms].clearValidate()
      this.$emit('operationReset')
    },
    closeDialog () {
      this.$refs.ruleForm.clearValidate()
      this.$emit('operationReset')
    },
    // 打开生成连接串的方法
    openConnectStrDialog () {
      this.connectStrDailog.dialogFormVisible = true
      this.connectStrDailog.category = this.ruleForm.source_category
    },
    // 生成连接串成功时
    buildStrSuccess (fromConnect) {
      this.connectStrDailog.dialogFormVisible = false
      this.ruleForm.source_category = fromConnect.category
      this.ruleForm.connect_str = fromConnect.connect_str
    },
    // 判断连接串是否有效
    testConnectStrMethod () {
      if (!this.ruleForm.connect_str) {
        this.$message({
          type: 'error',
          message: '连接串内容为空'
        })
        return
      }
      this.dialogLoading = true
      this.$post('/icpdatasource/test_connectstr', {
        category: this.ruleForm.source_category,
        connstr: this.ruleForm.connect_str
      }).then(data => {
        let dataResult = data.data.result
        if (dataResult.code === 'success') {
          this.$message({
            type: 'success',
            message: dataResult.msg
          })
        } else {
          this.$message({
            type: 'error',
            message: dataResult.msg
          })
        }
        this.dialogLoading = false
      }).catch(error => {
        this.dialogLoading = false
        this.$message({
          type: 'error',
          message: error
        })
      })
    },
    // 点击弹窗的确定按钮
    submitForm (forms) {
      this.$refs[forms].validate((valid) => {
        if (valid) {
          if (this.dialogInfo.operationType === 'edit') {
            this.editMethod()
          } else if (this.dialogInfo.operationType === 'add') {
            this.addMethod()
          }
        } else {
          return false
        }
      })
    }
  },
  watch: {
    'dialogInfo.dialogFormVisible': {
      handler: function (val, oldVal) {
        this.dialogFormVisible = val
        if (val) {
          this.initDialog()
        }
      },
      deep: true
    }
  }
}
